package com.pilipili.controller;

import com.alibaba.fastjson.JSON;
import com.pilipili.pojo.Users;
import com.pilipili.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;


@Controller
@SessionAttributes("msg")
public class UserController{
    @Autowired
    @Qualifier(value = "UserServiceImpl")
    private UserService userService;
    static Logger logger = Logger.getLogger(UserController.class);
//    public void setUserService(UserService userService) {
//        this.userService = userService;
//    }
    @RequestMapping("/home")
    public String toHome(){

        return "homepage";
    }

    @RequestMapping("/updateUser")
    public String updateUser(Model model,String username,String password,String name,String emile,String xb,String phone,String location){
        //System.out.println(username);
        model.addAttribute("username",username);
        model.addAttribute("password",password);
        model.addAttribute("name",name);
        model.addAttribute("emile",emile);
        model.addAttribute("xb",xb);
        model.addAttribute("phone",phone);
        model.addAttribute("location",location);
        return "updateUser";
    }

    @PostMapping("toUpdateUser")
    public String toUpdateUser(Model model,HttpServletRequest request, HttpServletResponse response){
      String username = request.getParameter("username");
      String password = request.getParameter("password");
        String name = request.getParameter("name");
        String emile = request.getParameter("emile");
        String xb = request.getParameter("xb");
        String phone = request.getParameter("phone");
        String location = request.getParameter("location");
        Users users = new Users(username, password, name, emile, xb, phone, location);

        int updateUser = userService.updateUser(users);

        if (updateUser>0){
            return "UserController";
        }else {
            model.addAttribute("updateTs","修改失败,请稍后重试!");
            return "updateUser";
        }
    }
@RequestMapping("/deleteUser")
@ResponseBody
public String deleteUser(Model model,String username){
    System.out.println("开始删除");
    int i = userService.deleteUser(username);
    LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
    //map.put("code",0);
    map.put("msg","200");
    //map.put("count",1);
    map.put("data",i);
    String s = JSON.toJSONString(map);
    System.out.println(s);
    return s;
}
    @PostMapping ("/zc")
    public String zc(Model model,HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name=request.getParameter("name");
        String emile =request.getParameter("emile");
        String xb =request.getParameter("xb");
        String phone =request.getParameter("phone");
        String location =request.getParameter("location");
          Users users = new Users(username, password, name, emile, xb,phone,location);
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//        UserService service = context.getBean("UserServiceImpl", UserService.class);
        try {
            int insert = userService.insert(users);
            if (insert>=0){
                model.addAttribute("zcts","注册成功!");
                return "zc";
            }else {
                model.addAttribute("zcts","输入有误,注册失败!");
                return "zc";
            }
        } catch (Exception e) {
            model.addAttribute("zcts","输入有误,注册失败!");
            return "zc";
        }

    }
    @RequestMapping("/hyzc")
    public String hyzc(HttpServletRequest request, HttpServletResponse response){

        return "insertuser";
    }
    @RequestMapping("/grzy")
    public String grzy(HttpServletRequest request, HttpServletResponse response){

        return "个人主页";
    }
    @RequestMapping("/xxyl")
    public String xxyl(HttpServletRequest request, HttpServletResponse response){

        return "休闲娱乐";
    }
    @RequestMapping("/xxkj")
    public String xxkj(HttpServletRequest request, HttpServletResponse response){

        return "学习空间";
    }
    @RequestMapping("/yykj")
    public String yykj(HttpServletRequest request, HttpServletResponse response){

        return "应用空间";
    }
    @RequestMapping("/error")
    public String error(){
        return "error";
    }
    @RequestMapping("/userout")
    public String goOut(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        session.removeAttribute("user");
        int i = (int) session.getAttribute("OnlineCount");
        session.setAttribute("OnlineCount",i-1);
        return "redirect:/login.jsp";
    }

    @RequestMapping("/user")
    public String User(){
        return "UserController";
    }
    @RequestMapping("/UserController")
    @ResponseBody
    public String UserSelect(Model model){
        List<Users> user = new ArrayList<>();
        user = userService.selectAllUser();
        model.addAttribute("list",user);
        int size = user.size();
        model.addAttribute("size",size);
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",user);
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }
    @RequestMapping("/login")
    public String Login(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session){


        String username = request.getParameter("user");
        String password = request.getParameter("password");
        Users users = userService.selectUser(username);
        logger.info("开始进入");
        if (users!=null&&users.getPassword().equals(password)){
            session.setAttribute("name",users.getName());
            System.out.println("======>"+session.getAttribute("name"));
            Enumeration e = session.getAttributeNames();
            int i = 0;
            while (e.hasMoreElements()) {
                e.nextElement();
                i++;
            }
            session.setAttribute("OnlineCount",i);
            return "homepage";
        }else {
            model.addAttribute("msg","账号/密码输入错误!!");
            return "redirect:/login.jsp";
        }
    }
}
